//
//  LEOTabBar.swift
//  SinaWeibo
//
//  Created by 李子海LEO on 4/6/16.
//  Copyright © 2016 李子海LEO. All rights reserved.
//

import UIKit

class LEOTabBar: UITabBar {
    
    var plusButton:UIButton
    
    
    override init(frame: CGRect) {
        
        let plusButton = UIButton()
        
        self.plusButton=plusButton
        
        super.init(frame: frame)

        plusButton.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), forState: .Normal)
        
        plusButton.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        plusButton.setImage(UIImage.init(named: "tabbar_compose_icon_add"), forState: .Normal)
        
        plusButton.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        addSubview(plusButton)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let buttonW:CGFloat = self.frame.width/5
        
        let buttonH:CGFloat = self.frame.height

        let rect = CGRectMake(0, 0, buttonW, buttonH)
        
        var index:CGFloat = 0
        
        for subView in subviews {

            //如果控件是按钮,那么修改按钮的 frame

            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {

                subView.frame=CGRectOffset(rect, index*buttonW, 0)
               
//                print(index)
                
                if index==2 {
 
                    index++
                    
                    subView.frame=CGRectOffset(rect, index*buttonW, 0)
                
                }
                
                index++
            }

            plusButton.frame=CGRectOffset(rect, 2*buttonW, 0)
        }
        
    }
}
